import request from '@/utils/request'
import store from '@/store'

// 登录 ------ 接口
export const loginAPI = ({ mobile, code }) => {
  return request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })
}

// 频道  ------ 获取所有列表
export const allChannelListAPI = () => {
  // 接口方法, 只负责调用一个接口, 返回一个Promise对象
  return request({
    url: '/v1_0/channels'
  })
}

// 首页  -----  获取用户已选导航标题
export const allUserDaoHangAPI = () => {
  return request({
    url: '/v1_0/user/channels',
    headers: {
      Authorization: 'Bearer ' + store.state.token
    }
  })
}

// 首页  -----  更新用户已选导航标题
export const gengXinUserDaoHangAPI = ({ channels }) => {
  return request({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels: channels
    }
  })
}

// 首页  ------ 获取文章列表
export const getWenZhangAPL = ({ channelId, timestamp }) => {
  return request({
    url: '/v1_0/articles',
    params: {
      channel_id: channelId,
      timestamp: timestamp || new Date().getTime()
    }
  })
}

// 不感兴趣 -------
export const buGanXinQuAPI = ({ delId }) => {
  return request({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + store.state.token
    },
    data: {
      target: delId
    }
  })
}

// 二级接口 举报 -----------
export const juBaoAPI = ({ delId, type }) => {
  return request({
    url: '/v1_0/article/reports',
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + store.state.token
    },
    data: {
      target: delId,
      type: type + '',
      // 其它问题
      remark: type === 0 ? '其它问题......' : null
    }
  })
}

// 搜索页 --- 联想菜单
export const lianXiangAPL = ({ q }) => {
  return request({
    url: '/v1_0/suggestion',
    params: {
      q: q
    }
  })
}

// 搜索 --- 结果列表
export const souSuoJieGuoListAPI = ({ q, page = 1 }) => {
  return request({
    url: '/v1_0/search',
    params: {
      q,
      page
    }
  })
}

// 文章 ----- 指定文章详情
export const wenZhangXQAPI = ({ id }) => {
  return request({
    // url: '/v1_0/articles/' + id
    url: `/v1_0/articles/${id}`
  })
}

// 文章 ------ 关注作者
export const guanZhuAPI = ({ target }) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })
}

// 文章 ------ 取消关注作者
export const quGuanAPI = ({ id }) => {
  return request({
    url: '/v1_0/user/followings/' + id,
    method: 'DELETE'
  })
}

// 文章 ------ 喜欢点赞
export const dianZanAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })
}

// 文章 ------ 不喜欢 取消点赞
export const buDianZanAPI = ({ id }) => {
  return request({
    url: '/v1_0/article/likings/' + id,
    method: 'DELETE'
  })
}

// 评论 ------ 获取列表
export const pingLunAPI = ({ artId, offset = null, limit = 10 }) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })
}

// 评论 --- 点赞
export const hongXinAPI = ({ id }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: id
    }
  })
}

// 评论 --- 取消点赞
export const noHongXinAPI = ({ id }) => {
  return request({
    url: '/v1_0/comment/likings/' + id,
    method: 'DELETE'
  })
}

// 评论 ------ 发布评论
export const faBuAPI = ({ id, content }) => {
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: id,
      content
    }
  })
}

// 用户 ------ 基本资料
export const userJiBenAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 用户 ------ 个人资料
export const userGeRenAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 用户 ------ 更换头像
// data 要的是一个表单对象 new FromData()表单对象
export const touXiangAPI = (fromObj) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: fromObj
  })
}

// 用户 ------ 更新资料
export const gengXinUserAPI = ({ birthday, username }) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday,
      name: username
    }
  })
}
